<?php
include("./API.php");
include("../config.php");
tongji("ping");
$hh=$_GET['hh']?:"\n";//换行符号(默认\n)
if($_GET['url']==null){
echo "".$web["ming"]."API♧Ping网站测速".$hh."";
echo "━━━━━━━━━".$hh."";
echo "请输入参数！".$hh."";
echo "━━━━━━━━━".$hh."";
echo "Tips:".$web["ming"]."API技术支持";
exit();
}
if (preg_match('/http:\/\//i', $_GET['url'])|preg_match('/https:\/\//i', $_GET['url'])) {
echo "".$web["ming"]."API♧Ping网站测速".$hh."";
echo "━━━━━━━━━".$hh."";
echo '不能带http://或https://，只需要输入域名！'.$hh.'';
echo "━━━━━━━━━".$hh."";
echo "Tips:".$web["ming"]."API技术支持";
exit();
}
$msg = $_GET['url'];
$host = $msg;
$port = '80';
$num = 2; //Ping次数
$tip = gethostbyname($_GET['url']);
//获取时间
function mt_f (){
list($usec,$sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec); //微秒加秒
}
function ping_f($host,$port){
$time_s = mt_f();
$ip = gethostbyname($host);
$fp = @fsockopen($host,$port);
if(!$fp)
return '测试超时';
$get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnect:".$port."Close\r\n";
fputs($fp,$get);
fclose($fp);
$time_e = mt_f();
$time = $time_e - $time_s;
$time = ceil($time * 1000);
return $time;
}
if(ping_f($host,$port)=='测试超时'){
echo "".$web["ming"]."API♧Ping网站测速".$hh."";
echo "━━━━━━━━━".$hh."";
echo '测试超时，请检查目标站点是否正常！'.$hh.'';
echo "━━━━━━━━━".$hh."";
echo "Tips:".$web["ming"]."API技术支持";
}else{
for($i = 0;$i < $num;$i++){
if($i==0){
$s1=$t.ping_f($host,$port);
}elseif($i == 1){
$s2=$t.ping_f($host,$port);
}
//每次运行中间间隔1S
sleep(1);
//刷新输出缓存
ob_flush();
flush();
}
$se=json_decode(file_get_contents("http://opendata.baidu.com/api.php?query=".$tip."&co=&resource_id=6006&t=1433920989928&ie=utf8&oe=utf-8&format=json"),true);
$dz=$se["data"][0]["location"];
if($s1>$s2){
echo "".$web["ming"]."API♧Ping网站测速".$hh."";
echo "━━━━━━━━━".$hh."";
echo '查询数据：'.$msg.''.$hh.'';
echo 'IP地址：'.$tip.''.$hh.'';
echo '主机地址：'.$dz.''.$hh.'';
echo '最慢：'.$s1.'/ms'.$hh.'';
echo '最快：'.$s2.'/ms'.$hh.'';
echo '平均：'.(($s1-$s2)/2+$s2).'/ms'.$hh.'';
echo "━━━━━━━━━".$hh."";
echo "Tips:".$web["ming"]."API技术支持";
}else{
echo "".$web["ming"]."API♧Ping网站测速".$hh."";
echo "━━━━━━━━━".$hh."";
echo '查询数据：'.$msg.''.$hh.'';
echo 'IP地址：'.$tip.''.$hh.'';
echo '主机地址：'.$dz.''.$hh.'';
echo '最慢：'.$s1.'/ms'.$hh.'';
echo '最快：'.$s2.'/ms'.$hh.'';
echo '平均：'.(($s1-$s2)/2+$s2).'/ms'.$hh.'';
echo "━━━━━━━━━".$hh."";
echo "Tips:".$web["ming"]."API技术支持";
}
}
?>